package com.gagakuai.engine.impl.interceptor;

import com.gagakuai.engine.impl.cfg.TransactionPropagation;

/*
 * @program: common-starters
 * @description: 命令配置类，用来做事务配置
 * @author: houhong
 * @create: 2023-01-01 22:39
 */
public class CommandConfig {

    /*
     * 上下文是否可用
     */
    private boolean contextReusePossible;
    /*
     * 事务传播行为
     */
    private TransactionPropagation propagation;



    public CommandConfig() {
        this.contextReusePossible = true;
        this.propagation = TransactionPropagation.REQUIRED;
    }

    public CommandConfig(boolean contextReusePossible) {
        this.contextReusePossible = contextReusePossible;
        this.propagation = TransactionPropagation.REQUIRED;
    }

    public CommandConfig(boolean contextReusePossible, TransactionPropagation transactionPropagation) {
        this.contextReusePossible = contextReusePossible;
        this.propagation = transactionPropagation;
    }

    protected CommandConfig(CommandConfig commandConfig) {
        this.contextReusePossible = commandConfig.contextReusePossible;
        this.propagation = commandConfig.propagation;
    }

    public boolean isContextReusePossible() {
        return contextReusePossible;
    }

    public TransactionPropagation getTransactionPropagation() {
        return propagation;
    }

    public CommandConfig setContextReusePossible(boolean contextReusePossible) {
        CommandConfig config = new CommandConfig(this);
        config.contextReusePossible = contextReusePossible;
        return config;
    }

    public CommandConfig transactionRequired() {
        CommandConfig config = new CommandConfig(this);
        config.propagation = TransactionPropagation.REQUIRED;
        return config;
    }

    public CommandConfig transactionRequiresNew() {
        CommandConfig config = new CommandConfig();
        config.contextReusePossible = false;
        config.propagation = TransactionPropagation.REQUIRES_NEW;
        return config;
    }

    public CommandConfig transactionNotSupported() {
        CommandConfig config = new CommandConfig();
        config.contextReusePossible = false;
        config.propagation = TransactionPropagation.NOT_SUPPORTED;
        return config;
    }
}